package com.allianz.webapps.controllers;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.allianz.webapps.https.interfaces.appBeneficiaries.pojo.BeneficiariesModel;
import com.allianz.webapps.https.interfaces.appHQ.AppHQ;
import com.allianz.webapps.orderspojo.App;
import com.allianz.webapps.orderspojo.AppBase;
import com.fasterxml.jackson.databind.ObjectMapper;

@Controller
@RequestMapping(value="main")
public class AppHQController {

	@Autowired
	private ObjectMapper objectMapper;
	@Autowired
	private AppHQ appHQ;
	
	/**
	 * 编辑的请求
	 */
	@RequestMapping(value="editHQ")
	@ResponseBody
	public App editHQ(@RequestParam(value="jsonParam") String jsonParam,HttpServletRequest request,HttpServletResponse response)
			throws Exception{
		AppBase appBase=(AppBase)objectMapper.readValue(jsonParam, AppBase.class);
		String body=appHQ.requestEditParam(appBase);
		JSONObject json=new JSONObject(body);
		App app=null;
		String body1="";
		if (json.get("Body").equals(null)) {
			if(!("S").equals(json.getString("Status")))
				throw new Exception(json.getString("ErrMsg"));
			else
				return null;
		}else{
			body1=json.getString("Body");
			app=objectMapper.readValue(body1, App.class);
		}
		return app;
	}
	
	/**
	 * 保存的请求    成功的处理还没搞好
	 */
	@RequestMapping(value="saveHQ",produces = "text/html;charset=UTF-8")
	@ResponseBody
	public void saveHQ(@RequestParam(value="jsonParam") String jsonParam,HttpServletRequest request,HttpServletResponse response)
			throws Exception{
		App app=(App)objectMapper.readValue(jsonParam, App.class);
		String body=appHQ.requestSaveParam(app);
		JSONObject json=new JSONObject(body);
		if(!("S").equals(json.getString("Status")))
			throw new Exception(json.getString("ErrMsg"));
	}
	
}
